import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/base/base_state.dart';
import 'package:flutter_module/base/base_stateful_widget.dart';

class TestDialog extends BaseStatefulWidget {
  @override
  _TestDialogState createState() => _TestDialogState();
}

class _TestDialogState extends BaseState<TestDialog> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("对话框相关"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: [
            SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                    onPressed: () {
                      showCommondDialog(context);
                    },
                    child: Text("通用对话框"))),
            SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                    onPressed: () {
                      showCustomDialog(context);
                    },
                    child: Text("自定义对话框")))
          ],
        ),
      ),
    );
  }

  //通用对话框使用
  void showCommondDialog(BuildContext context) async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("提示"),
            content: Text("确定要删除吗？"),
            actions: [
              TextButton(
                  child: Text("取消"),
                  onPressed: () {
                    Navigator.pop(context, "cancel");
                  }),
              TextButton(
                  child: Text("确定"),
                  onPressed: () {
                    Navigator.pop(context, "yes");
                  })
            ],
          );
        });
    showToast(result);
  }

  void showCustomDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (context) {
          return CustomDialog();
        });
  }
}

class CustomDialog extends Dialog {
  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: MediaQuery.of(context).size.width * 2 / 3,
          color: Colors.green,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text("确定要退出已登录账号"),
              Divider(
                height: 1,
                color: Colors.grey,
              ),
              Row(
                children: [
                  Expanded(child: Text("取消")),
                  Divider(
                    height: 1,
                    color: Colors.grey,
                  ),
                  Expanded(child: Text("确定")),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
